﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace NCKH.Personal
{
    public partial class nhomungdung : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hienDonvi();
                hienDonviTV();
                HienDSTK();
                hiendetai();
            }
        }
        protected void hiendetai()
        {
            string connect = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string select_detai = "SELECT MaDT, TenDT FROM DETAI WHERE Trangthai=N'đã nghiệm thu'";
            using (SqlConnection cnn = new SqlConnection(connect))
            {
                using (SqlCommand cmd = new SqlCommand(select_detai, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                        {
                            while (rd.Read())
                            {
                                drddetai.Items.Add(new ListItem(rd["TenDT"].ToString(), rd["MaDT"].ToString()));
                                drtendetai.Items.Add(new ListItem(rd["TenDT"].ToString(), rd["MaDT"].ToString()));
                            }
                        }
                        rd.Close();
                    }
                }
                cnn.Close();
            }
        }

        protected void TVNhomNNC_Insert(string Vaitro, int ManhomUD, string MaNNC)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("insert_thanhviennhomUD_NNC", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@Vaitro", Vaitro);
                    Cmd.Parameters.AddWithValue("@ManhomUD", ManhomUD);
                    Cmd.Parameters.AddWithValue("@MaNNC", MaNNC);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected void TVNhomSV_Insert(string Vaitro, int ManhomUD, string MaSV)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("insert_thanhviennhomUD_SV", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@Vaitro", Vaitro);
                    Cmd.Parameters.AddWithValue("@ManhomUD", ManhomUD);
                    Cmd.Parameters.AddWithValue("@MaSV", MaSV);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected int layManhomUD()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlManhom = "Select ManhomUD From NhomUD Where TennhomUD='" + txtTennhom.Text + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlManhom, Cnn))
                {
                    int ManhomUD = 0;
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    ManhomUD = (int)Cmd.ExecuteScalar();
                    return ManhomUD;
                }
            }
        }
        protected string LayMaNNC(int TaikhoanID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("MaNNC_Select", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaTK", TaikhoanID);
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        string NNCID = null;
                        if (tbl.Rows.Count > 0)
                        {
                            DataRow dr = tbl.Rows[0];
                            object mannc = dr["MaNNC"];
                            NNCID = (string)mannc;
                        }
                        return NNCID;
                    }
                }
            }
        }
        protected string LayMaSV(int TaikhoanID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("MaSV_Select", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaTK", TaikhoanID);
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        string SvienID = null;
                        if (tbl.Rows.Count > 0)
                        {
                            DataRow dr = tbl.Rows[0];
                            object masv = dr["MaSV"];
                            SvienID = (string)masv;

                        }
                        return SvienID;
                    }
                }
            }
        }
        protected void HienDSTK()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqllayTK = "Select MaTK,Tendangnhap,Email From TAIKHOAN";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqllayTK, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        grvHienDS.DataSource = tbl;
                        grvHienDS.DataBind();
                    }
                }
            }
        }
        protected void NhomUD_Insert()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("insert_nhomUD", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@TennhomUD", txtTennhom.Text);
                    Cmd.Parameters.AddWithValue("@MaDV", ddlDonvi.SelectedValue);
                    Cmd.Parameters.AddWithValue("@MaDT", drddetai.SelectedValue);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected void hienDonviTV()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlDonvi_Select = "Select MaDV, TenDV From DONVI";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlDonvi_Select, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        while (rd.HasRows && rd.Read())
                        {
                            ddlDonviTV.Items.Add(
                                new ListItem(rd["TenDV"].ToString(), rd["MaDV"].ToString()));
                            drdonvi.Items.Add(new ListItem(rd["TenDV"].ToString(), rd["MaDV"].ToString()));
                        }
                        rd.Close();

                    }
                    Cnn.Close();
                }
            }
        }
        protected void hienDonvi()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlDonvi_Select = "Select MaDV, TenDV From DONVI";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlDonvi_Select, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        while (rd.HasRows && rd.Read())
                        {
                            ddlDonvi.Items.Add(
                                new ListItem(rd["TenDV"].ToString(), rd["MaDV"].ToString()));
                        }
                        rd.Close();

                    }
                    Cnn.Close();
                }
            }
        }

        protected void hienNhom()
        {
            if (rbtnNhom.Checked)
            {
                plNhom.Visible = true;

            }
            else
                plNhom.Visible = false;
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (rbtnNhom.Checked)
            {
                pnnhomud.Visible = true;

            }
            else
                pnnhomud.Visible = false;
            //hienNhom();
        }

        protected void btnLapnhom_Click(object sender, EventArgs e)
        {
            NhomUD_Insert();
            plThanhvien.Visible = true;
            doitrangthaidetai();
        }
        protected void doitrangthaidetai()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string update_trangthai = "UPDATE DETAI SET Trangthai=N'đang ứng dụng' WHERE MaDT='" + drddetai.SelectedValue + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(update_trangthai, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }

        protected void ddlDonvi_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnTim_Click(object sender, EventArgs e)
        {
            hientheodonvi();
        }
        protected void hientheodonvi()
        {
            string connect = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
        }

        protected void grvHienDS_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void grvHienDS_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("them"))
            {
                //string Vaitro = ((DropDownList)row.FindControl("ddlVaitro")).SelectedItem.Text;
                int RowIndex = int.Parse(e.CommandArgument.ToString().Trim());
                DropDownList ddlVaitro = (DropDownList)grvHienDS.Rows[RowIndex].FindControl("ddlVaitro");

                string Vaitro = ddlVaitro.SelectedItem.Text;
                /*if (Vaitro != null)
                {
                    lblthongbao.Text = "Item Found: " + Vaitro;
                }
                else
                {
                    lblthongbao.Text = "Item not Found: " + Vaitro;
                }*/
                int TaikhoanID = Convert.ToInt32(e.CommandArgument);
                string MaSV = LayMaSV(TaikhoanID);
                string MaNNC = LayMaNNC(TaikhoanID);
                int ManhomUD = layManhomUD();

                if (MaSV != null)
                {
                    TVNhomSV_Insert(Vaitro, ManhomUD, MaSV);

                }
                if (MaNNC != null)
                {
                    TVNhomNNC_Insert(Vaitro, ManhomUD, MaNNC);

                }
            }
        }
        public int count;
        protected void grvHienDS_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //DataRowView oRow = (DataRowView)e.Row.DataItem;

                ImageButton lnkPostType = (ImageButton)e.Row.FindControl("lbntThem");

                lnkPostType.CommandArgument = count.ToString();

                count++;

            }
        }

        protected void grvHienDS_SelectedIndexChanged1(object sender, EventArgs e)
        {
        }

        protected void rbtnNhom_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void rbtroi_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void btndongy_Click(object sender, EventArgs e)
        {
            if (rbtroi.Checked)
            {
                pnchonnhom.Visible = true;

            }
            else
                plNhom.Visible = true;
        }

        protected void rbtco_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void grvthanhviennhom_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void grvthanhviennhom_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btndetai_Click(object sender, EventArgs e)
        {
            bnthanhviennhom.Visible = true;
            doitrangthaidetai();
            themnhomUD();

        }
        protected void themnhomUD()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("insert_nhomUD", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@TennhomUD", drtendetai.SelectedItem.Text);
                    Cmd.Parameters.AddWithValue("@MaDV", drdonvi.SelectedValue);
                    Cmd.Parameters.AddWithValue("@MaDT", drtendetai.SelectedValue);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }

        protected void btntendetai_Click(object sender, EventArgs e)
        {
            if (rbtco.Checked)
            {
                bnnhomthanhvien.Visible = true;
                hienthanhviennhom();
            }
            else
            {
                Response.Write("<Script>alert('Bạn đã đăng kí nhóm ứng dụng thành công!' )</Script>");
            }
        }
        protected void hienthanhviennhom()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqllayTK = "Select MaTK,Tendangnhap,Email From TAIKHOAN WHERE MaQ=3";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqllayTK, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        grvthanhviennhom.DataSource = tbl;
                        grvthanhviennhom.DataBind();
                    }
                }
            }
        }

        protected void drtendetai_Load(object sender, EventArgs e)
        {

        }

        protected void drtendetai_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}